//
//  PainterCrosshairs.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/16.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterCrosshairsView.h"

using namespace ytx;

@implementation PainterCrosshairsView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _dx = nil;
        _dy = nil;
        _isBottomText = NO;
    }
    return self;
}


-(BOOL) hasData
{
    return [super hasData] && !(self.dx == nil && self.dy == nil);
}

- (void) drawCrosshairs:(ViewModelIndexPosition *)viewModel withPainter:(PainterBase *) painter withX:(NSNumber *) dx withY:(NSNumber *) dy
{
    self.indexPosition = viewModel;
    self.painter = painter;
    self.data = viewModel->getData();
    _dx = dx;
    _dy = dy;
    [self setNeedsDisplay];
}

- (void) drawCrosshairs:(ViewModelIndexPosition *)viewModel withPainter:(PainterBase *) painter withX:(NSNumber *) dx
{
    self.indexPosition = viewModel;
    self.painter = painter;
    self.data = viewModel->getData();
    _dx = dx;
    [self setNeedsDisplay];
}

- (void) drawCrosshairs:(ytx::ViewModelIndexPosition *)viewModel withPainter:(PainterBase *) painter withX:(NSNumber *) dx withY:(NSNumber *) dy isDrawBottomText:(BOOL) isBottomText
{
    _isBottomText = isBottomText;
    [self drawCrosshairs:viewModel withPainter:painter withX:dx withY:dy];
}

- (void)drawRect:(CGRect)cgrect
{
    if (![self hasData]){
        return;
    }
    
    // Drawing code.
    //获得处理的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    ChartCanvas * ctx = [ChartCanvas new];
    ctx.context = context;
    ctx.bounds = self.bounds;
    
    ViewModelIndexPosition * indexPosition = self.indexPosition;
    
    PainterCrosshairs * painter = (PainterCrosshairs *) self.painter;
    
    //YTXConfig * config = YTXConfig::getInstance();
    //ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    if (self.dx != nil) {
        int index = indexPosition->getViewIndexByPX([self.dx floatValue]);
        CGFloat x =  indexPosition->getViewPxByViewIndex(index) + rect.getZoomX() * 0.5;
        
        [painter drawCrosshairsX:cgrect withContext: ctx withViewModelIndexPosition:indexPosition withPx:x];
        
//        if (self.isBottomText) {
//            [painter drawCrosshairsXText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:x withIndex:index];
//        }

//        if (![NSStringFromClass(painter.class) hasSuffix:@"AVG"]) {//非AVG类型的视图才标记横轴的时间
            [painter drawCrosshairsYTextInKlinePainter:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:x withValue:0 withTimeType:1 withIndex:index];
//        }
    }
    
    if(self.dy != nil)
    {
        CGFloat y =  [self.dy floatValue];
        y = Tools::between(rect.getBeginY(), rect.getEndY(), y);
        [painter drawCrosshairsY:cgrect withContext: ctx withViewModelIndexPosition:indexPosition withPx:y];
        double value = indexPosition->getRectangle().getValueY(y);
        NSString * text = [NSString stringWithUTF8String:Tools::Convert2FixString( value, indexPosition->getData()->getToFix() ).c_str()];
        [painter drawCrosshairsYText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:y withValue:value withText:text];
    }
    
}

- (void)clear
{
    _dx = nil;
    _dy = nil;
    _isBottomText = NO;
    [super clear];
}

@end
